<?php
    // --------------------------------------------------------------------
    require_once $pathToRoot . "Common/Assignment.php";
    require_once $pathToRoot . "Common/Validate.php";
    
    require_once $pathToRoot . "Model/AssignmentHandler.php";
    require_once $pathToRoot . "Model/RegisterHandler.php";
    require_once $pathToRoot . "Model/FileUploadHandler.php";
    
    require_once $pathToRoot . "View/AssignmentView.php";
    require_once $pathToRoot . "View/RegisterView.php";
    require_once $pathToRoot . "View/FileUploadView.php";

    require_once $pathToRoot . "Controller/RegisterController.php";
    require_once $pathToRoot . "Controller/AssignmentController.php";
    // --------------------------------------------------------------------
        
	/**
	 * Klass LOGIN PAGES CONTROLLER - Används för att kontrollera vilken sida användaren vill komma åt när han/hon är inloggad.
	 * Controllern bestämmer vad som ska skrivas ut till användaren genom att "prata" med vyn och handlern. 
	*/
	class LoginPagesController
	{
		private $loginHandler = null;
		private $database = null;
		private $validate = null;
		private $loginActionView = null;
		
		private $assignmentController = null;
		private $assignmentHandler = null;
		private $assignmentView = null;

		private $registerHandler = null;
		private $registerView = null;

		private $fileuploadHandler = null;
		private $fileuploadView = null;


		//----- FUNKTION: CONSTRUCT --------------------------------------------------------------------------------------------------------
		/**
		* Konstruktor funktion som tilldelar objekt till de privata variablerna.
		*/
		public function __construct(LoginHandler $loginHandler, Database $database)
		{
			$this->loginHandler = $loginHandler;
			$this->database = $database;
			$this->validate = new Validate();
			$this->loginActionView = new LoginActionView();
			
			$this->assignmentController = new AssignmentController();
			$this->assignmentHandler = new AssignmentHandler($database, $this->validate);
			$this->assignmentView = new AssignmentView();			
			
			$this->registerHandler = new RegisterHandler($database, $this->validate);
			$this->registerView = new RegisterView();
			
			$this->fileuploadHandler = new FileUploadHandler($database);
			$this->fileuploadView = new FileUploadView();
		}


		//----- FUNKTION: DO LOGIN PAGES CONTROLL ------------------------------------------------------------------------------------------
		/**
		* Funktion som kontrollerar vilken sida användaren försöker komma åt. Och retunerar lämpligt innehåll.
		* @param string $currentPage Den aktuella sidan som användaren är inne på.
		* @return string $returnstring Innehåller informationen om den sidan som användaren har valt att gå till.
		*/
		public function DoLoginPagesControll($currentPage)
		{
			// Först kontrollerar vi om användaren är inloggad.
			if ($this->loginHandler->UserIsLoggedIn()) {
				$returnString = "";
				
				// Skickar in $currentPage som är den sidan som användaren vill gå in på.				
				switch ($currentPage) {
					// ----- AVSLUTADE UPPDRAG - ADMIN/KUND --------------------------------------------------------------------------------
					case LoginActionView::COMPLETED_ASSIGNMENT:
						if ($this->loginHandler->UserIsAdmin()) {
							$emptystring = "";
							$mainContent = $this->loginActionView->DoListCustomerCompletedAssignments($emptystring);
							$menu = $this->loginActionView->DoAdminMenu();
							$returnString = $this->loginActionView->CreateAdminView($menu, $mainContent);
						} else {
							$menu = $this->loginActionView->DoCostumerMenu();
							$customerID = intval($this->loginHandler->GetLoggedInCustomerID());
							$assignmentList = $this->assignmentHandler->GetCompletedCustomerAssignmentsFromDatabase($customerID);
							$assignmentListHTML = $this->assignmentView->ListCustomerCompletedAssignments($assignmentList);
							$mainContent = $this->loginActionView->DoListCustomerCompletedAssignments($assignmentListHTML);
							$returnString = $this->loginActionView->CreateCostumerView($menu, $mainContent);
						}
						break;

					// ----- LÄGG TILL UPPDRAG - ADMIN -------------------------------------------------------------------------------------
					case LoginActionView::CREATE_ASSIGNMENT:
						$mainContent = $this->assignmentController->DoAssignmentControll($this->database, $this->loginHandler);
						if ($this->loginHandler->UserIsAdmin()) {
							$menu = $this->loginActionView->DoAdminMenu();
							$returnString = $this->loginActionView->CreateAdminView($menu, $mainContent);
						} else {
							$menu = $this->loginActionView->DoCostumerMenu();
							$returnString = $this->loginActionView->CreateCostumerView($menu, $mainContent);
						}
						break;

					// ----- INKOMNA FAKTUROR - KUND ---------------------------------------------------------------------------------------
					case LoginActionView::INVOICE:
						
						if ($this->loginHandler->UserIsAdmin()) {
							$mainContent = $this->loginActionView->DoInvoice();
							$menu = $this->loginActionView->DoAdminMenu();
							$returnString = $this->loginActionView->CreateAdminView($menu, $mainContent);
						} else {
							$path = "../uploadedInvoice/";
							$customerID = $this->loginHandler->GetLoggedInCustomerID();
							$files = $this->fileuploadHandler->GetInvoicesByCustomerID($customerID);
							$fileList = $this->fileuploadView->ListAllFiles($files, $path);
							$mainContent = $this->loginActionView->DoInvoice($fileList);
							$menu = $this->loginActionView->DoCostumerMenu();
							$returnString = $this->loginActionView->CreateCostumerView($menu, $mainContent);
						}
						break;

					// ----- PÅGÅENDE UPPDRAG - ADMIN/KUND ---------------------------------------------------------------------------------
					case LoginActionView::ON_GOING_ASSIGNMENT:
						if ($this->loginHandler->UserIsAdmin()) {
							$emptystring = "";
							$mainContent = $this->loginActionView->DoListCustomerOngoingAssignments($emptystring);
							$menu = $this->loginActionView->DoAdminMenu();
							$returnString = $this->loginActionView->CreateAdminView($menu, $mainContent);
						} else {
							$menu = $this->loginActionView->DoCostumerMenu();
							$customerID = intval($this->loginHandler->GetLoggedInCustomerID());
							$assignmentList = $this->assignmentHandler->GetOngoingCustomerAssignmentsFromDatabase($customerID);
							$assignmentListHTML = $this->assignmentView->ListCustomerOngoingAssignments($assignmentList);
							$mainContent = $this->loginActionView->DoListCustomerOngoingAssignments($assignmentListHTML);
							$returnString = $this->loginActionView->CreateCostumerView($menu, $mainContent);
						}
						break;

					// ----- LADDA UPP FAKTURA - ADMIN -------------------------------------------------------------------------------------
					case LoginActionView::UPLOAD_INVOICE:
						$fileUploadController = new FileUploadController();
						$fileUploadForm = $fileUploadController->DoFileUploadControll($this->loginHandler, $this->database);					

						$existingCustomers = $this->loginHandler->GetAllCustomers();
						$existingCustomersDropDown = $this->assignmentView->DoExistingCustomerChoiceDropDown($existingCustomers);

						$mainContent = $this->loginActionView->DoUploadInvoice($fileUploadForm, $existingCustomersDropDown);
						if ($this->loginHandler->UserIsAdmin()) {
							$menu = $this->loginActionView->DoAdminMenu();
							$returnString = $this->loginActionView->CreateAdminView($menu, $mainContent);
						} else {
							$menu = $this->loginActionView->DoCostumerMenu();
							$returnString = $this->loginActionView->CreateCostumerView($menu, $mainContent);
						}
						break;

					// ----- SKAPA NY KUND - ADMIN -----------------------------------------------------------------------------------------
					case LoginActionView::NEW_CUSTOMER:
						$registerController = new RegisterController($this->validate);
						$registerForm = $registerController->DoRegisterControll($this->loginHandler, $this->registerHandler, $this->registerView);

						$mainContent = $this->loginActionView->DoNewCustomer($registerForm);
						if ($this->loginHandler->UserIsAdmin()) {
							$menu = $this->loginActionView->DoAdminMenu();
							$returnString = $this->loginActionView->CreateAdminView($menu, $mainContent);
						} else {
							$menu = $this->loginActionView->DoCostumerMenu();
							$returnString = $this->loginActionView->CreateCostumerView($menu, $mainContent);
						}
						break;

					// ---------------------------------------------------------------------------------------------------------------------
					default:
						break;
				}
				return $returnString;
			// Annars skickas vi till login-sidan.	
			} else {
				header("Location: ../login.php");
			}
		}
	}
?>